const path = require("path")
const { VueLoaderPlugin } = require('vue-loader')
const htmlWebpackPlugin = require('html-webpack-plugin')
const MiniCssExtractPlugin = require("mini-css-extract-plugin")

module.exports = {
  target: 'web', // webpack5 热更新
  entry: './src/main.js',
  optimization: {
    chunkIds: 'deterministic'
  },
  output: {
    path: path.resolve(__dirname, "dist"),
    filename: 'js/[name].js',
    assetModuleFilename: 'images/[hash][ext][query]',
    publicPath: '/',
    clean: true, // 清理 dist
  },
  resolve: {
    alias: {
      "@": path.resolve("src")
    }
  },
  module: {
    rules: [
      {
        test: /\.vue$/,
        use: 'vue-loader',
      },
      {
        test: /\.js$/,
        exclude: /node_modules/,
        use: [
          {
            loader: 'babel-loader'
          }
        ]
      },
      {
        test: /\.css$/,
        use: [
          MiniCssExtractPlugin.loader,
          "css-loader",
        ],
      },
      {
        test: /\.less$/,
        use: [
          MiniCssExtractPlugin.loader,
          "css-loader",
          "less-loader",
        ],
      },
      {
        test: /\.(png|jpg|jpeg|gif)$/,
        type: 'asset', // 等同 url-loader + limit
        parser: {
          dataUrlCondition: {
            maxSize: 10 * 1024
          }
        },
      },
    ]
  },
  plugins: [
    new VueLoaderPlugin(),
    new htmlWebpackPlugin({
      template: './public/index.html',
      favicon: path.resolve(__dirname, './public/favicon.ico')
    }),
    new MiniCssExtractPlugin({
      filename: "css/main.css",
    })
  ]
}